package hall.app.test.web.client;

import hall.app.customwidgets.library.client.jquerytab.JQTabPanel;
import hall.app.customwidgets.library.client.layout.LayoutType;
import hall.app.customwidgets.library.client.layout.ThreeColumnLayout;
import hall.app.customwidgets.library.client.layout.TwoColumnLayoutWithLeftNav;
import hall.app.customwidgets.library.client.layout.TwoColumnLayoutWithRightNav;
import hall.app.customwidgets.library.client.listwidgets.ListDefinition;
import hall.app.customwidgets.library.client.tab.TabbedPanel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.Callback;
import com.google.gwt.core.client.ScriptInjector;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.ScriptElement;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;

public class ClientWidgetUtil {

	static FlowPanel flowPanel = new FlowPanel();
	static Map<String, HTMLPanel> tabs2ContainerMap = new HashMap<String, HTMLPanel>();
	
	public static Composite getLayout(LayoutType layoutType)
	{
		switch (layoutType) {
		case TWO_LAYOUT_COLUMN_LEFT:
			return new TwoColumnLayoutWithLeftNav();
			
		case TWO_LAYOUT_COLUMN_RIGHT:
			return new TwoColumnLayoutWithRightNav();

		default:
			return new ThreeColumnLayout();
		}
	}
	
	public static void scriptInjection()
	{
		Element head = Document.get().getElementsByTagName("body").getItem(0);

		ScriptElement tabs = Document.get().createScriptElement();
		tabs.setType("text/javascript");
		tabs.setSrc("/tabs.js");
		head.appendChild(tabs);
		
		ScriptInjector.fromUrl("/tabs.js").setCallback(new Callback<Void, Exception>() {
			
			public void onSuccess(Void result) {
			}
			
			public void onFailure(Exception reason) {
				Window.alert("Script load failed.");				
			}
		}).inject();
	}
	
	public static Widget getJSTabLayout() {
		TabbedPanel tabbedPanel = new TabbedPanel();
		
		List<ListDefinition> listDefinitions = getListDefinitions();
		tabbedPanel.addListItems(listDefinitions);
		
		return tabbedPanel;
	}

	private static List<ListDefinition> getListDefinitions() {
		List<ListDefinition> listDefinitions = new ArrayList<ListDefinition>();
		
		listDefinitions.add(getListDefinition("List", "#list", true));
		listDefinitions.add(getListDefinition("Photos", "#photos", false));
		listDefinitions.add(getListDefinition("Maps", "#maps", false));
		listDefinitions.add(getListDefinition("Ananth", "#ananth", false));
		listDefinitions.add(getListDefinition("Raghu", "#raghu", false));
		return listDefinitions;
	}

	private static ListDefinition getListDefinition(String title, String target, boolean isSelected) {
		ListDefinition listDefinition = new ListDefinition();
		
		listDefinition.setTitle(title);
		listDefinition.setTarget(target);
		listDefinition.setSelected(isSelected);
		
		return listDefinition;
	}
	
}
